package cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.vue.api;

import cn.iocoder.yudao.framework.common.genCode.common.StringBuilderHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenColumnDO;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenTableDO;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.GlobalBinding;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.LocalBinding;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.PathHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.CodeGenTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class Api_js implements CodeGenTemplate {

    public String getPath(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        PathHandler pathHandler = PathHandler.builder()
                .table(table)
                .columns(columns)
                .globalBinding(globalBinding)
                .localBinding(localBinding)
                .primaryColumn(primaryColumn)
                .build();
        return pathHandler.vueFilePath("api/" + table.getModuleName() + "/" + localBinding.getClassNameVar() + ".js");
    }

    public String render(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        StringBuilderHandler code = new StringBuilderHandler();
        code.apAndBl("import request from '@/utils/request'");
        code.bl();
        code.apAndBl("// 创建" + table.getClassComment() + "");
        code.apAndBl("export function create" + localBinding.getSimpleClassName() + "(data) {");
        code.apAndBl("  return request({");
        code.apAndBl("    url: '" + "/" + table.getModuleName() + "/" + localBinding.getSimpleClassName_strikeCase() + "/create',");
        code.apAndBl("    method: 'post',");
        code.apAndBl("    data: data");
        code.apAndBl("  })");
        code.apAndBl("}");
        code.bl();
        code.apAndBl("// 更新" + table.getClassComment() + "");
        code.apAndBl("export function update" + localBinding.getSimpleClassName() + "(data) {");
        code.apAndBl("  return request({");
        code.apAndBl("    url: '" + "/" + table.getModuleName() + "/" + localBinding.getSimpleClassName_strikeCase() + "/update',");
        code.apAndBl("    method: 'put',");
        code.apAndBl("    data: data");
        code.apAndBl("  })");
        code.apAndBl("}");
        code.bl();
        code.apAndBl("// 删除" + table.getClassComment() + "");
        code.apAndBl("export function delete" + localBinding.getSimpleClassName() + "(id) {");
        code.apAndBl("  return request({");
        code.apAndBl("    url: '" + "/" + table.getModuleName() + "/" + localBinding.getSimpleClassName_strikeCase() + "/delete?id=' + id,");
        code.apAndBl("    method: 'delete'");
        code.apAndBl("  })");
        code.apAndBl("}");
        code.bl();
        code.apAndBl("// 获得" + table.getClassComment() + "");
        code.apAndBl("export function get" + localBinding.getSimpleClassName() + "(id) {");
        code.apAndBl("  return request({");
        code.apAndBl("    url: '" + "/" + table.getModuleName() + "/" + localBinding.getSimpleClassName_strikeCase() + "/get?id=' + id,");
        code.apAndBl("    method: 'get'");
        code.apAndBl("  })");
        code.apAndBl("}");
        code.bl();
        code.apAndBl("// 获得" + table.getClassComment() + "分页");
        code.apAndBl("export function get" + localBinding.getSimpleClassName() + "Page(query) {");
        code.apAndBl("  return request({");
        code.apAndBl("    url: '" + "/" + table.getModuleName() + "/" + localBinding.getSimpleClassName_strikeCase() + "/page',");
        code.apAndBl("    method: 'get',");
        code.apAndBl("    params: query");
        code.apAndBl("  })");
        code.apAndBl("}");
        code.bl();
        code.apAndBl("// 导出" + table.getClassComment() + " Excel");
        code.apAndBl("export function export" + localBinding.getSimpleClassName() + "Excel(query) {");
        code.apAndBl("  return request({");
        code.apAndBl("    url: '" + "/" + table.getModuleName() + "/" + localBinding.getSimpleClassName_strikeCase() + "/export-excel',");
        code.apAndBl("    method: 'get',");
        code.apAndBl("    params: query,");
        code.apAndBl("    responseType: 'blob'");
        code.apAndBl("  })");
        code.apAndBl("}");
        code.bl();

        return code.toString();
    }
}

